package com.pig4cloud.pig.environment.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.environment.entity.AlarmEntity;
import com.pig4cloud.pig.environment.service.AlarmService;
import com.pig4cloud.pig.environment.service.SubstationService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 报警信息
 *
 * @author pig
 * @date 2024-01-15 17:12:41
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarm" )
@Tag(description = "alarm" , name = "报警信息管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@Slf4j
public class AlarmController {

    private final  AlarmService alarmService;

	private final SubstationService substationService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param alarm 报警信息
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('environment_alarm_view')" )
    public R getAlarmPage(@ParameterObject Page page, @ParameterObject AlarmEntity alarm, HttpServletRequest request) {
        LambdaQueryWrapper<AlarmEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(alarm.getSubstationId()),AlarmEntity::getSubstationId,alarm.getSubstationId());
		wrapper.eq(Objects.nonNull(alarm.getEquipmentId()),AlarmEntity::getEquipmentId,alarm.getEquipmentId());
		wrapper.eq(StrUtil.isNotBlank(alarm.getFactorCode()),AlarmEntity::getFactorCode,alarm.getFactorCode());
		String alarmDate= request.getParameter("alarmDate");
		if(StrUtil.isNotBlank(alarmDate)){
			List<String> dateRang=JSONArray.parseArray(alarmDate,String.class);
			wrapper.between(AlarmEntity::getCreateTime,dateRang.get(0),dateRang.get(1));
			log.info("start:{} end:{}",dateRang.get(0),dateRang.get(1));

		}

        return R.ok(alarmService.findPage(page, wrapper));
    }


    /**
     * 通过id查询报警信息
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('environment_alarm_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(alarmService.getById(id));
    }

    /**
     * 新增报警信息
     * @param alarm 报警信息
     * @return R
     */
    @Operation(summary = "新增报警信息" , description = "新增报警信息" )
    @SysLog("新增报警信息" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('environment_alarm_add')" )
    public R save(@RequestBody AlarmEntity alarm) {
        return R.ok(alarmService.save(alarm));
    }

    /**
     * 修改报警信息
     * @param alarm 报警信息
     * @return R
     */
    @Operation(summary = "修改报警信息" , description = "修改报警信息" )
    @SysLog("修改报警信息" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('environment_alarm_edit')" )
    public R updateById(@RequestBody AlarmEntity alarm) {
		alarm.setHandlerTime(LocalDateTime.now());
		return R.ok(alarmService.updateById(alarm));
    }

    /**
     * 通过id删除报警信息
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除报警信息" , description = "通过id删除报警信息" )
    @SysLog("通过id删除报警信息" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('environment_alarm_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(alarmService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param alarm 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('environment_alarm_export')" )
    public List<AlarmEntity> export(AlarmEntity alarm,Long[] ids) {
        return alarmService.list(Wrappers.lambdaQuery(alarm).in(ArrayUtil.isNotEmpty(ids), AlarmEntity::getId, ids));
    }
}